var Utils = {

  sameArr(arr1,arr2){

    for (let i = 0; i < arr1.length; i++) {

       if(arr2.indexOf(arr1[i])  < 0){

         return false;
       }
    }
    for (let i = 0; i < arr2.length; i++) {
      if(arr1.indexOf(arr2[i]) < 0){

        return false;
      }
    }

    return true;
  },
  getMqttSerialNo() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
      s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";

    var uuid = s.join("");
    return uuid;
  },
  compare(property) {
    return function (obj1, obj2) {
      return obj1[property] - obj2[property];
    }
  },
  countMinutes(secondMin, fmt) {

    secondMin = parseInt(secondMin);

    let hour = Math.floor(secondMin / (60 * 60));
    secondMin = secondMin % (60 * 60);
    let minutes = Math.floor(secondMin / 60);
    let seconds = secondMin % 60;
    if (!fmt) {

      fmt = "mm分ss秒";
    }
    var o = {
      'h+': hour,
      'm+': minutes,
      's+': seconds
    };
    for (var k in o)
      if (new RegExp("(" + k + ")").test(fmt))
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));

    return {minutes, seconds, desc: fmt};
  },
  getA4Height(width) {

    return width * 297 / 210;
  },
  getA4Width(height) {

    return height * 210 / 297;
  },
  handleAnswerData(data, dotMap) {

    let period = 100;
    let arrData = data.split("|");
    let totalPage = 0;
    let maxTime = 0;

    let parseData = [];
    arrData.map((value)=>{
      let arr;
      try{
        if(value !== 0 && value!=='')arr = JSON.parse(value);else return;
        parseData.push({arr, time: (arr["stroke-start-time-s"] * 1000 + arr["stroke-start-time-ms"])})
      }catch(e){
        // console.error(e);
      }
    });
    if(parseData.length == 0){

      return dotMap|{};
    }
    parseData.sort(this.compare('time'));

    if (!dotMap || !dotMap.period) {

      dotMap = {period};//保存笔画的信息 页码 开始时间  结束时间  以及整理以后的轨迹点
    } else {

      totalPage = dotMap.totalPage;
      maxTime = Math.ceil(dotMap.maxTime * (1000 / period));
    }

    for (let i = 0; i < parseData.length; i++) {

      let arr;
      try {

        arr = parseData[i].arr;
      } catch (e) {
        continue;
        console.error(e)
      }
      let xyArr = arr['xy-dot-array'];

      let page = arr['code-page'];
      let beginTime = arr["stroke-start-time-s"];
      let beginTimeMs = arr["stroke-start-time-ms"];
      if (!dotMap.beginTime) {

        //将第一个点的时间记录为开始时间
        dotMap.beginTime = beginTime * 1000 + beginTimeMs;
      }

      if (!dotMap[page]) {

        dotMap[page] = {};
        totalPage++;
      }

      for (let j = 0; j < xyArr.length; j++) {

        /**
         let xy = xyArr[j];
         let x = 1.0 * (xy >> 16 & 0xFFFF) / 32 * 1.905;
         let y = 1.0 * (xy >> 0 & 0xFFFF) / 32 * 1.905;**/

        let x = xyArr[j].x;
        let y = xyArr[j].y;

        let time = parseInt(((j * 7 + beginTime * 1000 + beginTimeMs) - dotMap.beginTime) / period);

        if (time > maxTime) {

          maxTime = time;

        }
        if (!dotMap[page][time]) {

          dotMap[page][time] = [];
        }
        dotMap[page][time].push({x, y, idx:i});
      }

      dotMap.maxTime = Math.ceil(maxTime / (1000 / period));
      dotMap.totalPage = totalPage;
    }


    return dotMap;
  },
  handExamQuestionFmt(content) {

    if (!content) {
      return "";
    }
    content = content.split("##").join("【");
    content = content.split("$$").join("】");
    content = content.split("***").join("");
    content = content.split("<img").join("<img style='vertical-align:middle;'");
    return content;
  },
}


let prefixInteger = (num, length) => {
  return ("0000000000000000" + num).substr(-length);
}

let sortTree = function (data) {//将节点变为树结构

  var initLen = data[0].node.length;
  var res = [], curLen = 0, lastNode, lastNodeParent;
  for (var i = 0; i < data.length; i++) {

    if (data[i].node.length == initLen) {

      res.push(data[i]);
      lastNode = data[i];
      continue;
    }

    if (data[i].node.length > lastNode.node.length) {

      if (!lastNode.children) {

        lastNode.children = [];
      }
      lastNode.children.push(data[i]);
      lastNodeParent = lastNode;
      lastNode = data[i];
    } else if (data[i].node.length == lastNode.node.length) {

      lastNodeParent.children.push(data[i]);
      lastNode = data[i];
    } else if (data[i].node.length < lastNode.node.length) {

      lastNodeParent = res[res.length - 1];

      //找出节点属于哪个父节点
      while (data[i].node.length != lastNodeParent.children[0].node.length) {
        lastNodeParent = lastNodeParent.children[lastNodeParent.children.length - 1];
      }
      lastNodeParent.children.push(data[i]);
      lastNode = data[i];
    }
  }

  return res;
}

let toStrokeJSON = function (str) {

  let arr = str.split("|").filter(temp => {

    if (!temp) {
      return;
    }
    try {

      JSON.parse(temp)
      return true;
    } catch (e) {

      return false;
    }
  })

  return arr.map( temp => {

    return JSON.parse(temp);
  })
}




export {Utils, prefixInteger, sortTree,toStrokeJSON}
